import 'package:flutter_scaffold/flutter_scaffold.dart';
import 'package:get/get_connect/http/src/request/request.dart';

class BaseModel extends GetConnect {
  @override
  void onInit() {
    super.onInit();
    httpClient.addRequestModifier<void>((request) async {
      _logRequest(request);
      return request;
    });

    httpClient.addResponseModifier<void>((request, response) async {
      _logResponse(request, response);
      return response;
    });
  }

  // 记录请求详情
  void _logRequest(Request request) async {
    var body = await request.bodyBytes.bytesToString();
    Log.i('🌐 [${request.method}] ${request.url}\n请求头: ${request.headers}\n请求数据:${body}');
  }

  void _logResponse(Request request, Response response) {
    Log.i("✅ [${request.method}] ${request.url}\n状态码: ${response.statusCode}\n响应头:${response.headers}\n数据:${response.bodyString}");
  }
}
